package com.example.a0506below.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.a0506below.R;
import com.example.a0506below.Bean.Title;
import com.example.a0506below.fragments.adapter.MyRecylerShouYeTitleAdapter;
import com.example.a0506below.fragments.adapter.MyShouYeFvAdapter;
import com.example.a0506below.fragments.childfragment.MyNewsFragment;
import com.example.a0506below.fragments.childfragment.MyShehuiFragment;

import java.util.ArrayList;
import java.util.List;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

public class ShouYeFragment extends Fragment implements ViewPager.OnPageChangeListener {
    View view;
    RecyclerView recyclerView;
    MyRecylerShouYeTitleAdapter adapter;
    ViewPager viewPager;
    List<Title> titleList;
    List<Fragment> fragmentList;
    FragmentManager fragmentManager;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view==null) {
            view = inflater.inflate(R.layout.fragment_shouye, null);
            initList();
            initFragmentList();
            initView();

            recyclerView.setAdapter(adapter);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            layoutManager.setOrientation(RecyclerView.HORIZONTAL);
            recyclerView.setLayoutManager(layoutManager);
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent!=null){
            parent.removeView(view);
        }

        return view;
    }

    private void initList() {
        titleList = new ArrayList<Title>();
        titleList.add(new Title("头 条"));
        titleList.add(new Title("社 会"));
        titleList.add(new Title("国 内"));
        titleList.add(new Title("国 际"));
        titleList.add(new Title("娱 乐"));
        titleList.add(new Title("体 育"));
        titleList.add(new Title("军 事"));
        titleList.add(new Title("科 技"));
        titleList.add(new Title("财 经"));
        titleList.add(new Title("时 尚"));
    }

    private void initFragmentList() {
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new MyNewsFragment());
        fragmentList.add(new MyShehuiFragment());
    }

    private void initView() {
        recyclerView = view.findViewById(R.id.shouye_rclv);
        adapter = new MyRecylerShouYeTitleAdapter(titleList, getActivity());

        fragmentManager = getChildFragmentManager();

        viewPager = view.findViewById(R.id.viewpager01);
        viewPager.setAdapter(new MyShouYeFvAdapter(fragmentManager,0,fragmentList));
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(this);
        adapter.setOnItemClickListener(new MyRecylerShouYeTitleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                viewPager.setCurrentItem(position);
            }
        });
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        adapter.setBg(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
